--[[ Copyright 2012, Chris Ho                               ]]--
--[[ MIT License (Please read the LICENSE file for details) ]]--

Use("common/Const");
Use("common/Log");
Use("common/List");
Use("common/Position");

-- A clique is a group of related Creature instances. The clique
-- binds object IDs to Creature instances.
Clique = {};

--[[ Load a clique from a file ]]--
function Clique.Load(_filename)
  local Read, status = loadfile(_filename);
  if (Read == nil) then
    Alert("Clique.Load() " .. _filename .. ": " .. status);
    return {};
  else
    Info("Clique.Load() loaded " .. _filename);
    return Read();
  end
end

--[[ Save a clique to a file. ]]--
function Clique.Save(_filename, _listing)
  local file = io.open(_filename, "w");
  if (file ~= nil) then
    local prefix = "";

    file:write("return { ");
    for key, value in ipairs(_listing) do
      file:write(prefix .. "[" .. key .. "] = Creature.Create(" .. key .. ")");
      prefix = ", ";
    end
    file:write(" };");

    file:close();
    Info("Clique.Save() wrote " .. _filename);
  else
    Alert("Clique.Save() " .. _filename .. ": failed");
  end
end
